package com.slz3.business.module.configManagement.conActivityForecast.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.configManagement.conActivityForecast.domain.ConActivityForecast;
import com.slz3.business.module.configManagement.conActivityForecast.domain.ConActivityForecastFactory;
import com.slz3.business.module.configManagement.conActivityForecast.steward.ConActivityForecastSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class ConActivityForecastServiceImpl implements ConActivityForecastService {

    @Override
    public ConActivityForecast save(ConActivityForecast conActivityForecast) {
        return Repositorys.conActivityForecast.save(ConActivityForecastFactory.initBaseFields(conActivityForecast));
    }

    @Override
    public Page<ConActivityForecast> page(MultiValueMap<String, String> params) {
        Page<ConActivityForecast> conActivityForecastPage = Repositorys.conActivityForecast.findAll(
                ConActivityForecastSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return conActivityForecastPage;
    }

    @Override
    public ConActivityForecast modify(ConActivityForecast conActivityForecastNew) {
        return Repositorys.conActivityForecast.saveAndFlush(ConActivityForecastFactory.initBaseFields(conActivityForecastNew));
    }

    @Override
    public Optional<ConActivityForecast> single(UUID uuid) {
        return Repositorys.conActivityForecast.findById(uuid);
    }

    @Override
    public List<ConActivityForecast> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.conActivityForecast.findAllById(uuids);
    }

    @Override
    public List<ConActivityForecast> delete(List<ConActivityForecast> conActivityForecastList) {
        conActivityForecastList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            ConActivityForecastFactory.initBaseFields(item);
        });
        return Services.conActivityForecastTransactional.batchModify(conActivityForecastList);
    }

}
